
public class BankruptcyChecker implements CountInterface {

	private Player p;
	private Player newOwner;
	private Block[] bl;
	private PropertyBlock[] propArray;
	private Utilities[] utilArray;
	private RailRoad[] railArray;
	private int totalWorth;
	
	public BankruptcyChecker(Player play, Block[] block){
		this.p=play;
		this.bl=block;
		this.totalWorth=0;
		
		this.utilArray = new Utilities[iUtilCount];
		this.railArray = new RailRoad[iRailCount];
		this.propArray = new PropertyBlock[iPropCount];
		
		fillBlockArrays();
		checkPlayerWorth();
	}
	
	public void fillBlockArrays(){
		int tempPropBlockNum = 0;
		int tempRailRoadNum = 0;
		int tempUtilityNum = 0;
		
		for(int i=0;i<this.bl.length;i++){
			if(bl[i] instanceof Utilities){
				this.utilArray[tempUtilityNum] = (Utilities)bl[i];
				tempUtilityNum++;
			}else if(bl[i] instanceof RailRoad){
				this.railArray[tempRailRoadNum] = (RailRoad)bl[i];
				tempRailRoadNum++;
			}else if(bl[i] instanceof PropertyBlock){
				this.propArray[tempPropBlockNum] = (PropertyBlock)bl[i];
				tempPropBlockNum++;
			}
		}
	}
	
	public int checkPlayerWorth(){
		for(int i=0; i<this.utilArray.length ; i++){
			if(this.utilArray[i].getCOwner()==this.p && this.utilArray[i].getIsMortgaged()!=true){
				this.totalWorth=totalWorth+this.utilArray[i].getMortgageValue();
			}
		}
		
		for(int i=0; i<this.railArray.length ; i++){
			if(this.railArray[i].getCOwner()==this.p && this.railArray[i].getIsMortgaged()!=true){
				this.totalWorth=totalWorth+this.railArray[i].getMortgageValue();
			}
		}
		
		for(int i=0; i<this.propArray.length ; i++){
			if(this.propArray[i].getCOwner()==this.p && this.propArray[i].getIsMortgaged()!=true){
				this.totalWorth=totalWorth+(this.propArray[i].getCurrentNumberOfHouses()*(this.propArray[i].getCostOfHouse()/2));
				this.totalWorth=totalWorth+this.propArray[i].getMortgageValue();
			}
		}
		
		this.totalWorth=this.totalWorth+this.p.getCurrentMoney();
		return totalWorth;
	}
	
	public void assignNewOwner(){
		if(this.p.getCurrentBlockPos() instanceof RailRoad){
			RailRoad rail = (RailRoad)this.p.getCurrentBlockPos();
			this.newOwner=rail.getCOwner();
		}else if(this.p.getCurrentBlockPos() instanceof Utilities){
			Utilities util = (Utilities)this.p.getCurrentBlockPos();
			this.newOwner=util.getCOwner();
		}else if(this.p.getCurrentBlockPos() instanceof PropertyBlock){
			PropertyBlock prop = (PropertyBlock)this.p.getCurrentBlockPos();
			this.newOwner=prop.getCOwner();
		}
	}
	
	public void transferAssetsToPlayer(){
		assignNewOwner();
		
		for(int i=0; i<this.utilArray.length ; i++){
			if(this.utilArray[i].getCOwner()==this.p){
				this.utilArray[i].setCOwner(this.newOwner);
			}
		}
		
		for(int i=0; i<this.railArray.length ; i++){
			if(this.railArray[i].getCOwner()==this.p){
				this.railArray[i].setCOwner(this.newOwner);
			}
		}
		
		for(int i=0; i<this.propArray.length ; i++){
			if(this.propArray[i].getCOwner()==this.p){
				this.propArray[i].setCOwner(this.newOwner);
			}
		}
	
		if(this.p.getCurrentMoney()>0){
			this.newOwner.addMoney(this.p.getCurrentMoney());
			this.p.subtractMoney(this.p.getCurrentMoney());
		}
	}
	
	
	 public void transferAssetsToBank(Block[] bl, Player[] play, Player p){


		 AuctionUI auctionUI = new AuctionUI(bl, play, p);

		 if(this.p.getCurrentMoney()>0){
		 this.p.subtractMoney(this.p.getCurrentMoney());
		 }


		 }


}
